<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Models\Group;
use Illuminate\Support\Facades\Auth;

class GroupRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $user = Auth::User();
        $groupId = $this->route('group');
        if (!empty($groupId)) {
            if (!$user->can('update', new Group())) {
                abort(403, '此操作未经授权.');
            }
        } else {
            if (!$user->can('store',  new Group())) {
                abort(403, '此操作未经授权.');
            }
        }
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|between:1,25',
            'parent_id'=>'required',
            'email' => 'unique:groups',
        ];
    }
}
